package cn.kgc.ebookvue02.controller;

import cn.kgc.ebookvue02.pojo.Entry;
import cn.kgc.ebookvue02.pojo.ResultEntity;
import cn.kgc.ebookvue02.service.EntryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-19 22:47
 */
@RestController
public class EntryContoroller {

    @Resource
    EntryService entryService;

    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Entry>> selectEntry(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSzie", defaultValue = "3", required = false) Integer pageSize,
            @RequestParam(value = "categoryId", defaultValue = "0", required = false) Integer categryId){
        PageInfo<Entry> pageInfo = entryService.selectEntryPageinfo(pageNum, pageSize, categryId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    //前端post请求 参数是json格式必须添加@requestBody
    @RequestMapping("/saveEntry")//有id是修改  没id事添加
    public ResultEntity<Integer> saveEntry(@RequestBody Entry entry){
        Integer integer=0;
        if(entry.getId()==null){//添加
            entryService.addEntry(entry);
        }else{//修改
            entryService.updateEntry(entry);
        }
        return ResultEntity.successWithoutData(integer);
    }

    //根据id查询图书
    @RequestMapping("/getEntryById")
    public ResultEntity<Entry> getEntryById(Integer id){
        Entry entry = entryService.selectById(id);
        return ResultEntity.successWithoutData(entry);
    }
        //删除
    @RequestMapping("/deleteEntry")
    public ResultEntity<Integer> deleteEntry(Integer id){
        Integer integer = entryService.deleteEntry(id);

        return ResultEntity.successWithoutData(integer);
    }
}
